import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"
import AutoImport from "unplugin-auto-import/vite";
import ESLintPlugin from "vite-plugin-eslint"
import dts from "vite-plugin-dts"


export default defineConfig({
  plugins: [
    vue({
      script: {
        defineModel: true,
        propsDestructure: true
      }
    }),
    AutoImport({
      include: [
        /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
        /\.vue$/, /\.vue\?vue/, // .vue
      ],
      exclude: ['./dist'],
      imports: ["vue"],
      dts: './auto-imports.d.ts',
      eslintrc: {
        enabled: true, // Default `false`
        filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`s
        globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
      }
    }),
    ESLintPlugin(),
    dts({
      outDir: ["dist"],
      /**
       * TODO 需要把自定义的 d.ts 导入不然会 报 类型找不到
       * auto-imports.d.ts AutoImport 生成的Vue Api
       * components.d.ts 自定义d.ts
       */
      include: ["./src","auto-imports.d.ts","global.d.ts"],
      exclude: ["./dist", "./docs", "./example", "./theme-chalk", "node_modules"],
      staticImport: true,
      pathsToAliases: false
    }),
  ],
  resolve: {
    alias: {
      "ys-crud": _resolve("./src/"),
      "@playground": _resolve("./playground/"),
    },
  },
  build: {
    lib: {
      // 入口文件
      entry: path.resolve(__dirname, './src/index.ts'),
      // 文件名称
      name: 'ys-plus',
      formats: ['es'],
      fileName: (format) => {
        return `${format}/index.js`
      },
    },
    rollupOptions: {
      external: ['vue', 'vue-router','element-plus'],
      output: {
        exports: 'named',
        globals: {
          vue: 'Vue'
        }
      }
    }
  }
})


function _resolve(dir: string) {
  return path.resolve(__dirname, dir)
}
